// components/search/index.js
import {
  KeywordModel
} from '../../models/keyword.js'

import {
  BookModel
} from '../../models/book.js'

import {
  paginationBev
} from '../behaviors/pagination.js'

const keywordModel = new KeywordModel()
const bookModel = new BookModel()

Component({
  /**
   * 组件的属性列表
   */
  behaviors: [paginationBev],
  properties: {
    more: {
      type: Boolean,
      observer: 'loadMore'
    },
    historyWords: Array,
    hotWords: Array
  },

  /**
   * 组件的初始数据
   */
  data: {
    // historyWords: [],
    // hotWords: []
    // dataArray: [],
    searching: false,
    q: '',
    // total: 0,
    // loading: false,
    loadingCenter: false
  },

  attached() {
    // this.setData({
    //   historyWords: keywordModel.getHistory()
    // })
    // keywordModel.getHot().then(res => {
    //   this.setData({
    //     hotWords: res.hot
    //   })
    // })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    loadMore() {
      if (!this.data.q) {
        return
      }
      if (this.isLocked()) {
        //正在发送请求中，用户同时发送多个请求只能处理一个
        return
      }
      if (this.hasMore()) {
        this.locked()
        bookModel.search(this.getCurrentStart(), this.data.q).then(res => {
          this.setNewData(res.books)
          this.unLocked()
        }, () => {
          this.unLocked()
        })
      }
    },

    onCancel(event) {
      this.triggerEvent('cancel', {}, {})
    },

    onDelete(event) {
      this.setData({
        searching: false,
        q: '',
        noneResult: false
      })
    },

    onConfirm(event) {
      this.initialize()
      //显示用户搜索回车后的搜索页面
      const q = this._showElement(event)
      if (!q) {
        return
      }
      // this._showLoadingCenter()
      //加载搜索页面的内容
      this._showContent(q)
    },

    // _isLocked() {
    //   return this.data.loading ? true : false
    // },

    // _locked() {
    //   this.setData({
    //     loading: true
    //   })
    // },

    // _unLocked() {
    //   this.setData({
    //     loading: false
    //   })
    // },

    _showLoadingCenter() {
      this.setData({
        loadingCenter: true
      })
    },

    _hideLoadingCenter() {
      this.setData({
        loadingCenter: false
      })
    },

    _showElement(event) {
      const q = event.detail.value || event.detail.text
      if (!q) {
        wx.showToast({
          title: '输入不能为空',
          icon: 'none'
        })
        return
      }
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        searching: true
      })
      return q
    },

    _showContent(q) {
      bookModel.search(0, q).then(res => {
        wx.hideLoading()
        keywordModel.addToHistory(q)
        this.setNewData(res.books)
        this.setTotal(res.total)
        this.setData({
          q,
          // dataArray: res.books,
          // total: res.total,
          historyWords: keywordModel.getHistory()
        })
        // this._hideLoadingCenter()
      })
    }

    // scroll-view | Page onReachBottom   实现上划加载更多

  }
})